<?php

class RssPostImageManager
{
    public static function GetTableName()
    {
        return Util::GetDatabaseTableName("rss_post_image");
    }

    public static function GetRssPostImageById( $id )
    {
        $sql = 'SELECT * FROM rss_post_image WHERE post_image_id = %s LIMIT 1';
        $data = Core::DBConn()->Query( $sql, Util::Quote($id) )->GetAssoc();
        $result = ( !empty($data) ) ? self::CreateRssPostImage($data) : null;
        return $result;
    }

    public static function GetImageByPostId( $postId )
    {
        $sql = 'SELECT * FROM rss_post_image WHERE post_id = %s LIMIT 1';
        $data = Core::DBConn()->Query( $sql, Util::Quote($postId) )->GetAssoc();
        $result = ( !empty($data) ) ? self::CreateRssPostImage($data) : null;
        return $result;
    }

    public static function Insert( RssPostImage $rssPostImage )
    {
        $tableName = self::GetTableName();
        $sql = "INSERT IGNORE INTO $tableName
                                        (title, link, image, post_id)
                                    VALUE
                                        (@title, @link, @image, @post_id)";

        $params = array
        (
        	'@title' => $rssPostImage->GetTitle(),
            '@link' => $rssPostImage->GetLink(),
            '@image' => $rssPostImage->GetImage(),
            '@post_id' => $rssPostImage->GetPostId()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        $id = Core::DBConn()->GetLastInsertId();

        $rssPostImage->SetPostImageId($id);

        return $id;
    }

    public static function Update( RssPostImage $rssPostImage )
    {
        $tableName = self::GetTableName();
        $sql = "UPDATE $tableName
        		SET
        		  title = @title,
        		  link = @link,
        		  image = @image,
        		  post_id = @post_id
        		WHERE
        		  post_image_id = @post_image_id";

        $params = array
        (
        	'@title' => $rssPostImage->GetTitle(),
            '@link' => $rssPostImage->GetLink(),
            '@image' => $rssPostImage->GetImage(),
            '@post_id' => $rssPostImage->GetPostId(),
            '@post_image_id' => $rssPostImage->GetPostImageId()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        return $rssPostImage->GetPostImageId();
    }

    public static function Save( RssPostImage $rssPostImage )
    {
        if ( $rssPostImage->GetPostImageId() == null )
        {
            self::Insert($rssPostImage);
        }
        else
        {
            self::Update($rssPostImage);
        }

        return $rssPostImage->GetPostImageId();
    }

    public static function Delete( $postImageId )
    {
        $sql = 'DELETE FROM rss_post_image WHERE post_image_id = %s';
        Core::DBConn()->Query( $sql, Util::Quote($postImageId) );
    }

    public static function SaveAsThumbnail(RssPostImage $postImage, $postId)
    {
        $upload_dir = wp_upload_dir();
        $image_data = $postImage->GetImage();
        $filename = basename($postImage->GetLink());
        if (wp_mkdir_p($upload_dir['path']))
            $file = $upload_dir['path'] . '/' . $filename;
        else
            $file = $upload_dir['basedir'] . '/' . $filename;
        file_put_contents($file, $image_data);

        $wp_filetype = wp_check_filetype($filename, null );
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => $filename,
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment( $attachment, $file, $postId );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
        wp_update_attachment_metadata( $attach_id, $attach_data );

        set_post_thumbnail( $postId, $attach_id );
    }

    private static function CreateRssPostImage( $data )
    {
        $rssPostImage = new RssPostImage();

        $rssPostImage->SetPostImageId($data['post_image_id']);
        $rssPostImage->SetPostId($data['post_id']);
        $rssPostImage->SetTitle($data['title']);
        $rssPostImage->SetLink($data['link']);
        $rssPostImage->SetImage($data['image']);

        return $rssPostImage;
    }

}

?>